﻿using Asmodat.Abbreviate;
using Asmodat.Extensions.Collections.Generic;
using Asmodat.Extensions.Objects;
using Asmodat.IO;
using Asmodat.Types;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Asmodat_ELM
{
    public partial class Form1 : Form
    {




        private void UpdateDictionaryControls()
        {
            string text = TTbxDictionary.Text.TryTrim();

            if (Dictionary.Contains(text))
                TBtnSave.Enabled = false;
            else
            {
                if (text.IsNullOrEmpty())
                    TBtnSave.Enabled = false;
                else
                    TBtnSave.Enabled = true;
            }

            if (DictionarySelectedKeys.IsNullOrEmpty())
                TBtnRemove.Enabled = false;
            else
                TBtnRemove.Enabled = true;
        }
        
        private void TTbxDictionary_TextChanged(object sender, EventArgs e)
        {
            this.UpdateDictionary();
            TDgbDictionary_SelectionChanged(sender, e);
        }

        List<string> Dictionary = new List<string>();
        private void TBtnSave_Click(object sender, EventArgs e)
        {
            string key = TTbxDictionary.Text.TryTrim();
            Dictionary.Add(key);
            this.UpdateDictionary();
            this.SaveState_Dictionary();

            TDgbDictionary_SelectionChanged(sender, e);
        }

        private void TBtnRemove_Click(object sender, EventArgs e)
        {
            if (DictionarySelectedKeys.IsNullOrEmpty())
                return;

            Dictionary.RemoveRange(DictionarySelectedKeys);
            this.UpdateDictionary();
            this.SaveState_Dictionary();

            TDgbDictionary_SelectionChanged(sender, e);
        }
        
        private void UpdateDictionary()
        {
            string mask = TTbxDictionary.Text.TryTrim();

            if (TDgbDictionary.Columns.Count <= 0)
                TDgbDictionary.AddTextColumns(new string[,] { { "Keys", "Keys", "Keys" } });

            TDgbDictionary.ClearRows();


            List<string> result;

            if (!mask.IsNullOrEmpty())
                result = Dictionary.Where(s => s != null && s.Contains(mask)).ToList();
            else
                result = Dictionary;

            result.Sort();

            foreach (string s in result)
            {
                TDgbDictionary.AddRow(new string[] { s });
            }

        }

        private void TDgbDictionary_OnCellClick(object source, Asmodat.FormsControls.ThreadedDataGridCellClickEvent e)
        {
            string key = e.Value?.ToString()?.TryTrim();

            if (key.IsNullOrEmpty())
                return;

            TTbxSubmit.Text = key;
        }

        List<string> DictionarySelectedKeys = new List<string>();
        private void TDgbDictionary_SelectionChanged(object sender, EventArgs e)
        {
            if (TDgbDictionary.RowCount <= 0)
                return;

            var cells = TDgbDictionary.SelectedCells;

            DictionarySelectedKeys.Clear();

            if (cells != null)
                for (int i = 0; i < cells.Count; i++)
                {
                    var key = cells[i].Value;
                    if (key == null)
                        continue;

                    DictionarySelectedKeys.Add(cells[i].Value.ToString());
                }

            UpdateDictionaryControls();
        }

    }
}
